#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 2010;
int a[N];
int f[N][N];
int n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int len = 1; len <= n; len++)
    {
        for (int i = 1; i + len - 1 <= n; i++)
        {
            int j = i + len - 1;
            int cnt = n - len + 1;
            f[i][j] = max(f[i + 1][j] + a[i] * cnt, f[i][j - 1] + a[j] * cnt);
        }
    }
    cout << f[1][n] << endl;
    return 0;
}